//
//  ViewController.m
//  开门红
//
//  Created by on 2017/5/15.
//  Copyright © 2017年 All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong)  UIView *containerView;

@property (nonatomic,strong) UIView *doorLayer;
@property (nonatomic,strong) UIView *doorLayer1;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.doorLayer = [UIView new];
    self.doorLayer.frame = CGRectMake(0, 0, CGRectGetWidth(self.containerView.frame)/2, CGRectGetHeight(self.containerView.frame));
    self.doorLayer.layer.position = CGPointMake(0, CGRectGetHeight(self.containerView.frame)/2);
    self.doorLayer.layer.anchorPoint = CGPointMake(0, 0.5);
    self.doorLayer.layer.contents = (__bridge id)[UIImage imageNamed:@"000.png"].CGImage;
    
    self.doorLayer1 = [UIView new];
    self.doorLayer1.frame = CGRectMake(0, 0, CGRectGetWidth(self.containerView.frame)/2, CGRectGetHeight(self.containerView.frame));
    self.doorLayer1.layer.position = CGPointMake(CGRectGetWidth(self.containerView.frame), CGRectGetHeight(self.containerView.frame)/2);
    self.doorLayer1.layer.anchorPoint = CGPointMake(0, 0.5);
    self.doorLayer1.layer.contents = (__bridge id)[UIImage imageNamed:@"000.png"].CGImage;
    self.doorLayer1.layer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
    
    UIImageView *ima = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"111"]];
    ima.frame = CGRectMake(10, 20, CGRectGetWidth(_containerView.frame), CGRectGetHeight(_containerView.frame));
    [self.view addSubview:ima];
    
    //容器视图
    [self.view addSubview:self.containerView];
   
    [_containerView addSubview:self.doorLayer];
    [_containerView addSubview:self.doorLayer1];
    CATransform3D perspective = CATransform3DIdentity;
    //透视效果，要操作的对象要有旋转的角度，不然没效果
    perspective.m34 = -1.0 / 2000.0;
    self.containerView.layer.sublayerTransform = perspective;
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"transform.rotation.y";
    animation.toValue = @(M_PI);
    animation.duration = 3.0;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    [self.doorLayer.layer addAnimation:animation forKey:nil];
    
    CABasicAnimation *animation1 = [CABasicAnimation animation];
    animation1.keyPath = @"transform.rotation.y";
    animation1.toValue = @(M_PI);
    animation1.duration = 3.0;
    animation1.removedOnCompletion = NO;
    animation1.fillMode = kCAFillModeForwards;
    [self.doorLayer1.layer addAnimation:animation1 forKey:nil];
    
}


-(UIView *)containerView
{
    if (!_containerView)
    {
        _containerView = [[UIView alloc]init];
        _containerView.center = self.view.center;
        _containerView.bounds = CGRectMake(0, 0, self.view.frame.size.width-20, self.view.frame.size.height-40);
    }
    return _containerView;
}


@end
